package fr.afcepf.atod.gestionVP.dao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.atod.gestionVP.data.NoteDeFrais;

@Component("myNoteDeFraisDao")  //pour une prise en charge par Spring
@Transactional
public class DaoNoteFraisImpl implements IDaoNoteFrais {


	//@PersistenceContext (d'origine Ejb3)
	//permet d'initialiser entityManager
	//en fonction de META-INF/persitence.xml
	@PersistenceContext(unitName="Gestion Vente Paiement")
	private EntityManager em;

//****************************************************************************************
	//Methode provenant de IDao
//****************************************************************************************


	public void createNoteFrais(NoteDeFrais nf) {
		em.persist(nf);

	}


	public void deleteNoteFrais(int id) {
		em.remove(this.getNoteFraisById(id));
	}


	public void updateNoteFrais (NoteDeFrais nf) {
		em.merge(nf);
	}



	@SuppressWarnings("unchecked")
	public Collection<NoteDeFrais> getAllNoteFrais(int id) {
		Query query = em.createQuery("SELECT nf FROM NoteDeFrais as nf WHERE nf.id =:param");
		query.setParameter("param", id);
		return query.getResultList();
	}

	public NoteDeFrais getNoteFraisById(int id){
		return em.find(NoteDeFrais.class, id);
	}




	}
